package com.cunshe.web.manage;

import com.cunshe.common.ResponseVO;
import com.cunshe.config.annotation.Login;
import com.cunshe.domain.User;
import com.cunshe.entity.vo.UserInsertVO;
import com.cunshe.entity.vo.UserUpdatePasswordVO;
import com.cunshe.entity.vo.UserUpdateVO;
import com.cunshe.service.UserService;
import com.cunshe.web.BaseController;
import com.sunkee.commons.security.md5.PasswordEncoder;
import com.sunkee.commons.utils.ConvertUtil;
import com.sunkee.commons.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 登陆用户
 * @author xiao
 */
@Api(tags = "登陆用户")
@Login
@RestController
@RequestMapping(value = "/manage/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    /** 新增 */
    @ApiOperation(value = "新增")
    @PostMapping(value = "insert")
    public ResponseVO insert(@RequestBody @Validated UserInsertVO vo) {
        User user = ConvertUtil.copyProperties(vo, User.class);
        user.setPassword(passwordEncoder.encode(vo.getPassword()));
        user.setCreateBy(getCurrentUser().getUsername());
        userService.insert(user);
        return ResponseVO.success();
    }

    /** 删除 */
    @ApiOperation(value = "删除")
    @PostMapping(value = "delete")
    public ResponseVO delete(@RequestParam("id") Long id) {
        userService.delete(id);
        return ResponseVO.success();
    }

    /** 修改 */
    @ApiOperation(value = "修改")
    @PostMapping(value = "update")
    public ResponseVO update(@RequestBody @Validated UserUpdateVO vo) {
        User user = ConvertUtil.copyProperties(vo, User.class);
        user.setUpdateBy(getCurrentUser().getUsername());
        userService.update(user);
        return ResponseVO.success();
    }

    /** 修改密码 */
    @ApiOperation(value = "修改密码")
    @PostMapping(value = "updatePassword")
    public ResponseVO updatePassword(@RequestBody @Validated UserUpdatePasswordVO vo) {
        if(!vo.getPassword1().equals(vo.getPassword2())){
            return ResponseVO.error(1, "两次密码输入不一致");
        }
        User user = ConvertUtil.copyProperties(vo, User.class);
        user.setId(getCurrentUser().getId());
        user.setPassword(passwordEncoder.encode(vo.getPassword1()));
        user.setUpdateBy(getCurrentUser().getUsername());
        userService.update(user);
        return ResponseVO.success();
    }

    /** 重置密码 */
    @ApiOperation(value = "重置密码")
    @PostMapping(value = "resetPassword")
    public ResponseVO resetPassword(@RequestParam("id") Long id) {
        String s = StringUtils.randomCode(6);
        User user = User.builder().id(id).password(passwordEncoder.encode(s)).build();
        user.setUpdateBy(getCurrentUser().getUsername());
        userService.update(user);
        return ResponseVO.success(s);
    }

    /** 查询列表 */
    @ApiOperation(value = "查询列表")
    @PostMapping(value = "list")
    public ResponseVO list(User record) {
        record = Optional.ofNullable(record).orElse(User.builder().build());
        record.setIdNotEquals(1L);
        List<User> list = userService.list(record);
        return ResponseVO.success(list);
    }

}